from django.db import models
# -*- coding: utf-8 -*-
# Create your models here.
from django.db.models import SET_NULL, ManyToManyField
from django.forms import DateField

import CarManage
import manage


class Driver(models.Model):
    id = models.CharField(max_length=20, primary_key=True)
    passwd = models.CharField(max_length=20, null=True, blank=True)
    name = models.CharField(max_length=20, null=True, blank=True)
    EntryTime = models.CharField(max_length=10, blank=True)
    WorkingState = models.CharField(max_length=10, default="在职", blank=True)
    StopServeDate = models.CharField(max_length=10, null=True, blank=True)
    StopTime = models.CharField(max_length=10, null=True, blank=True)
    DriverType = models.CharField(max_length=10,blank=True)
    ComplainedTimes = models.CharField(max_length=10, blank=True, default=0)
    sex = models.CharField(max_length=1,blank=True)
    Academic = models.CharField(max_length=20, null=True, blank=True)
    Experience = models.CharField(max_length=20, null=True, blank=True)
    phoneNum = models.CharField(max_length=11,blank=True)
    ManageID = models.CharField(max_length=20, null=True, blank=True)
    isRegister = models.BooleanField(default=False, blank=True)
    isLogin = models.BooleanField(default=False, blank=True)

    def to_dict(self, fields=('id', 'name', 'EntryTime',
                              'WorkingState', 'StopServeDate', 'StopTime',
                              'DriverType', 'ComplainedTimes', 'sex', 'Academic',
                              'Experience', 'phoneNum', 'ManageID'), exclude=None):
        return get_dict(self, fields, exclude)

    def to_dict8(self, fields='ComplainedTimes', exclude=None):
        return get_dict(self, fields, exclude)

    def to_dict9(self, fields=None, exclude=None):
        return get_dict(self, fields, exclude)


class Message(models.Model):
    id = models.AutoField(primary_key=True)
    ManageID = models.CharField(max_length=10, blank=True, null=True)
    driverID = models.CharField(max_length=10, blank=True, null=True)
    HandleTime = models.CharField(max_length=20, null=True, blank=True)
    Content = models.CharField(max_length=256, null=True, blank=True)
    Result = models.CharField(max_length=20, blank=True, null=True)


class FeedBack(models.Model):
    CusServeID = models.CharField(max_length=10, blank=True, null=True)
    DriverID = models.CharField(max_length=10, blank=True, null=True)
    messageType = models.CharField(max_length=10, blank=True, null=True)
    CustomerID = models.CharField(max_length=30, blank=True, null=True)
    Details = models.CharField(max_length=256, blank=True, null=True)
    ManagerID = models.CharField(max_length=10, blank=True, null=True)
    FeedBackState = models.CharField(max_length=20, blank=True, null=True)

    def to_dict2(self, fields=('CusServeID', 'DriverID', 'messageType',
                               'CustomerID', 'Details', 'ManagerID'), exclude=None):
        return get_dict(self, fields, exclude)




def get_dict(self, fields=None, exclude=None):
    data = {}
    for f in self._meta.concrete_fields + self._meta.many_to_many:
        value = f.value_from_object(self)
        if fields and f.name not in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if isinstance(f, ManyToManyField):
            value = [i.id for i in value] if self.pk else None
        if isinstance(f, DateField):
            value = value.strftime('%Y-%m-%d') if value else None
        data[f.name] = value
    return data
